//
//  LSAppDelegate.m
//  zuqiutiyu
//
//  Created by 刘文超 on 2020/3/23.
//  Copyright © 2020 刘文超. All rights reserved.
//

#import "LSAppDelegate.h"
#import "LS_webViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "LS_ShowLoadView.h"

#define SAVE_URL  @"SAVE_URL"
@implementation LSAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     
  
     NSLog(@"");
     [AVOSCloud setApplicationId:@"tywVveAVj3i2S2BbPkdonep7-gzGzoHsz"
                        clientKey:@"th9kbQJTakRpQyAvI8OKhLEE"
                  serverURLString:@"https://tywvveav.lc-cn-n1-shared.com"];
      
    
      
      //1.增加一个缓存策略 让加载的更快 如果有就加载有的 实时更新本地的
      NSString *url = [[NSUserDefaults standardUserDefaults] objectForKey:SAVE_URL];
      if (url.length == 0) {
          [self requUrlWithsaveKey:false];
      }else{
          [self pushOutSafari:url];
          [self requUrlWithsaveKey:true];
      }
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = [[LS_webViewController alloc] init];
    [self.window makeKeyAndVisible];
    
    [[LS_ShowLoadView sharedLS_ShowLoadView] showLoadView];
    return YES;
}
-(void)requUrlWithsaveKey:(BOOL)isUpdate
{
    AVQuery *query = [AVQuery queryWithClassName:@"data"];
      [query getObjectInBackgroundWithId:@"5e7c07e31358aa00856fdb9c" block:^(AVObject *todo, NSError *error) {
          NSLog(@"--------%@",todo[@"message"]);
          NSString *url = todo[@"message"][@"URL"];
          [[NSUserDefaults standardUserDefaults] setObject:url forKey:SAVE_URL];
          [[NSUserDefaults standardUserDefaults] synchronize];
          if (isUpdate) {
          }else{
              [self pushOutSafari:todo[@"message"][@"URL"]];
          }
         
      }];
}
-(void)pushOutSafari:(NSString *)url
{
    [[LS_ShowLoadView sharedLS_ShowLoadView] removeSubView];
    if (url.length == 0) {
        return;
    }
    if (@available(ios 10.0 , *)) {
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString:url] options: @{} completionHandler: nil];
    } else {
       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    }
}
@end
